﻿using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;

namespace Wedn.Web.Handler
{
    public static class HttpHandler
    {
        /// <summary>
        /// 加载资源文件
        /// </summary>
        /// <param name="context">Http请求上下文</param>
        /// <param name="dirs">加载的目录</param>
        /// <param name="files">加载的文件名</param>
        /// <param name="pathFormat">相对路径格式</param>
        public static void LoadResource(HttpContext context, string[] files, string pathFormat)
        {
            StringBuilder sbd = new StringBuilder();
            //DirectoryInfo dirInfo = new DirectoryInfo(context.Server.MapPath(dir));
            //FileInfo[] files = dirInfo.GetFiles(searchPattern, SearchOption.AllDirectories);
            //List<string> fns = new List<string>(fileNames);
            //foreach (var item in files)
            //{
            //    if (fns.Contains(Path.GetFileNameWithoutExtension(item.Name)))
            //    {
            //        sbd.AppendLine(File.ReadAllText(item.FullName));
            //    }
            //}
            foreach (string item in files)
            {
                string path = context.Server.MapPath(string.Format(pathFormat, item.Replace('.', '/')));
                if (File.Exists(path))
                    sbd.AppendLine(File.ReadAllText(path));
            }
            if (sbd.Length == 0)
            {
                context.Response.Clear();
                context.Response.End();
            }
            if (context.Response.ContentType != "text/css")
                context.Response.Write(sbd.ToString());
            else
                context.Response.Write(sbd.ToString().Replace("../", "../resource/"));
        }
    }
}
